//
//  HttpRequestManager.m
//  OneToOne
//
//  Created by Herui on 9/5/14.
//  Copyright (c) 2014 jiayuan. All rights reserved.
//

#import "HttpRequestManager.h"
#import "AFHTTPRequestOperationManager.h"
#import "JiaYuanAPIHelper.h"

// add by herui: 2014.9.5
// 采用AFNetworking 封装网络行为

@implementation HttpRequestManager
{
    AFHTTPRequestOperationManager *manager;
}



- (id)init
{
    self = [super init];
    if (self) {
        //
        manager = [AFHTTPRequestOperationManager manager];
    }
    return self;
}

+ (HttpRequestManager *)defaultManager
{
    static HttpRequestManager *manager = nil;
    if (manager == nil) {
        manager = [[HttpRequestManager alloc] init];
    }
    return manager;
}



+ (void)postWithUrlString:(NSString *)urlString
               parameters:(id)parameters
                  success:(void (^)(id))success
                  failure:(void (^)(NSError *))failure
{
    [self postWithUrlString:urlString parameters:parameters success:success failure:failure isLogin:YES];
}

+ (void)postWithUrlString:(NSString *)urlString
               parameters:(id)parameters
                  success:(void (^)(id response))success
                  failure:(void (^)(NSError *error))failure
                  isLogin:(BOOL)yesOrNo
{
    parameters = [JiaYuanAPIHelper APIDictWithURLString:urlString dic:parameters isLogin:yesOrNo];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager POST:urlString
       parameters:parameters
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              success(responseObject);
          }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              failure(error);
          }];
}

+ (void)postWithUrlString:(NSString *)urlString
               parameters:(id)parameters
                  success:(void (^)(id))success
                  failure:(void (^)(NSError *))failure
                    cache:(BOOL)yesOrNo
{
    
}

+ (void)getWithUrlString:(NSString *)urlString
                 success:(void (^)(id response))success
                 failure:(void (^)(NSError *error))failure
                 isLogin:(BOOL)yesOrNo
{
    [[self class] getWithUrlString:urlString success:success failure:failure cache:NO isLogin:yesOrNo];
}


+ (void)getWithUrlString:(NSString *)urlString
                 success:(void (^)(id))success
                 failure:(void (^)(NSError *))failure
{
    [[self class] getWithUrlString:urlString success:success failure:failure cache:NO];
}


+ (void)getWithUrlString:(NSString *)urlString
                 success:(void (^)(id))success
                 failure:(void (^)(NSError *))failure
                   cache:(BOOL)yesOrNo
{
    [[self class] getWithUrlString:urlString success:success failure:failure cache:yesOrNo isLogin:YES];
}


+ (void)getWithUrlString:(NSString *)urlString
                 success:(void (^)(id))success
                 failure:(void (^)(NSError *))failure
                   cache:(BOOL)yesOrNo
                 isLogin:(BOOL)isLogin
{
    // 先查看缓存, 如果存在使用缓存
//    if (yesOrNo) {
//        id cacheObject = nil;
//        cacheObject = [[TMCache sharedCache] objectForKey:urlString];
//        if (cacheObject) {
//            success(cacheObject);
//            return;
//        }
//    }
    
    NSString *string = [JiaYuanAPIHelper urlStringWithString:urlString isLogin:isLogin];
    // 没有缓存才发起网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSInteger code = [[responseObject objectForKey:@"code"] integerValue];
        if (code == 1) {
            if (yesOrNo) {
                // 将数据存入缓存
               //  [[TMCache sharedCache] setObject:responseObject forKey:urlString];
                // TODO 缓存机制
            }
        }
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}

+ (NSMutableURLRequest *)uploadRequestWithUrlString:(NSString *)urlString
                                         parameters:(id)parameters
                                           fileData:(id)fileData
                                               name:(NSString *)name
                                           fileName:(NSString *)fileName
                                           mimeType:(NSString *)mimeType
{
    parameters = [JiaYuanAPIHelper APIDictWithURLString:urlString dic:parameters isLogin:YES];
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:fileData name:name fileName:fileName mimeType:mimeType];
    } error:nil];
    return request;
    
}

+ (void)uploadFileWithUrlString:(NSString *)urlString
                     parameters:(id)parameters
                       fileData:(id)fileData
                           name:(NSString *)name
                       fileName:(NSString *)fileName
                       mimeType:(NSString *)mimeType
                        success:(void (^)(id))success
                        failure:(void (^)(NSError *))failure
{
    parameters = [JiaYuanAPIHelper APIDictWithURLString:urlString dic:parameters isLogin:YES];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    
    [manager POST:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:fileData name:name fileName:fileName mimeType:mimeType];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
    
}





@end
